/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: HomePageDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.qbooks.publisher;
 
import java.io.*;

import java.util.*;
import java.sql.*;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;



public class PublisherDAO
{
	
	public HashList selectPublisherList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/publisher.xml", "select_publisher_list", infos);
	}

	public HashList selectPublisherInfo(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_publisher_info", infos);
	}
	
	public HashList selectSalesList(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_sales_list", infos);
	}
	
	public HashList selectBookList(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_book_list", infos);
	}
	
	public HashList selectBookInfo(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_book_info", infos);
	}

	public HashList selectCouponList(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_coupon_list", infos);
	}
	
	public HashList selectCouponInfo(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_coupon_info", infos);
	}

	public HashList selectCouponNoList(HashMap infos) throws Exception {
        return DBManager.executeQuery("/publisher.xml", "select_coupon_no_list", infos);
	}
	
	
	
	public void savePublisher(HashBox box) throws Exception
	{
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			if ("C".equals(box.get("job_type"))) {
				DBManager.executeUpdate("/publisher.xml", "insert_publisher", box.getParam(), conn);
			} else if ("U".equals(box.get("job_type"))) {
				DBManager.executeUpdate("/publisher.xml", "update_publisher", box.getParam(), conn);
			}
			
			
			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
	}
	
	public String saveCoupon(HashBox box) throws Exception
	{
		Connection conn = null;
		String issue_id = "";
		
        try
		{
			conn = DBManager.getConnection();
			
			HashMap infos = box.getParam();

			if ("C".equals(box.get("job_type"))) {
				issue_id = DBManager.executeQuery("/publisher.xml", "select_issue_id", box.getParam(), conn).get("issue_id", 0);
				infos.put("issue_id", issue_id);
				DBManager.executeUpdate("/publisher.xml", "insert_coupon", infos, conn);
				int issueCount = box.getInt("issue_count");
				for (int i = 0; i < issueCount; i++) {
					infos.put("coupon_no", StringUtil.lpad(String.valueOf((long)(Math.random()*10000000000000000l)),16,"0"));
					try {
						DBManager.executeUpdate("/publisher.xml", "insert_coupon_no", infos, conn);
					} catch(SQLException e) {
						if (e.getMessage().indexOf("Duplicate entry") > -1) {
							issueCount--;
						}
					}
				}
			} else if ("U".equals(box.get("job_type"))) {
				issue_id = box.get("issue_id");
				DBManager.executeUpdate("/publisher.xml", "update_coupon", infos, conn);
			} else if ("D".equals(box.get("job_type"))) {
				issue_id = box.get("issue_id");
				DBManager.executeUpdate("/publisher.xml", "delete_coupon", infos, conn);
			}
			
			
			conn.commit();
			
			return issue_id;
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
	}
	
	
}
